/**
 * Copyright 2007-2009 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jadapter.func;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Implementation of the Iterator interfaces that joins
 * together multiple interators.
 * 
 * @author Vidar Svansson
 *
 * @param <T>
 */
/* Extracted form inner class in Query*/
public class JoinedIterator<T> implements Iterator<T>{

    private Iterator<? extends T>[] iterators;
    private int status = 0;
    private Iterator<? extends T> current;
    
    public JoinedIterator(Iterator<? extends T> ... iterators){
        this.iterators = iterators;
        this.current = iterators[status++];
    }
    
    public boolean hasNext() {
        if(current.hasNext()){
            return true;
        }
        if(status < iterators.length) {
            current = iterators[status++];
            return hasNext();
        } return false;           
    }

    public T next() {
        if(current.hasNext()){
            return current.next();
        }
        if(status < iterators.length) {
            current = iterators[status++];
            return next();
        } 
        throw new NoSuchElementException();
    }

    public void remove() {
        throw new UnsupportedOperationException("remove not supported");         
    }
    
}
